package com.framework.core.rest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author shenmengchao
 * @version 1.6.30
 * @date 2017/7/7
 * @description
 */

public class HttpUtils {

    public String mBaseUrl = "http://api.danghongyun.com/";//该地址由app/build.gradle里面HTTP_HOST参数配置，这里作为默认地址

    private static HttpUtils mInstance;
    private OkHttpClient mOkHttpClient;
    private Retrofit mRetrofit;

    private HttpUtils() {

    }

    public static HttpUtils getInstance() {
        if (null == mInstance) {
            mInstance = new HttpUtils();
        }
        return mInstance;
    }

    public HttpUtils setBaseUrl(String baseUrl) {
        this.mBaseUrl = baseUrl;
        return this;
    }

    public void init() {
        Gson gson = new GsonBuilder()
//                .registerTypeAdapterFactory(new ItemTypeAdapterFactory())
                .create();
        //build okhttp client
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        mOkHttpClient = builder
                .addNetworkInterceptor(new SignInterceptor())
                .addNetworkInterceptor(loggingInterceptor)
                .build();

        //build retrofit client
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mBaseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxTransformErrorCallAdapterFactory.create())
                .client(mOkHttpClient)
                .build();
    }

    public Retrofit getRetrofitClient() {
        return mRetrofit;
    }

    public static void main(String args[]) {
        int r = (int) (Math.random() * 7) + 20;
        System.out.println(r);
    }

}
